home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / ShowDib8 / ShowDib8.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  7.1 KB  |  232 lines

  1. /*--------------------------------------------------
  2.    SHOWDIB8.C -- Shows DIB converted to DIB section
  3.                  (c) Charles Petzold, 1998
  4.   --------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "..\\ShowDib3\\PackeDib.h"
  8. #include "resource.h"
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. TCHAR szAppName[] = TEXT ("ShowDib8") ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16. {
  17.      HWND     hwnd ;
  18.      MSG      msg ;
  19.      WNDCLASS wndclass ;
  20.  
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = szAppName ;
  30.      wndclass.lpszClassName = szAppName ;
  31.  
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.  
  39.      hwnd = CreateWindow (szAppName, TEXT ("Show DIB #8: DIB Section"),
  40.                           WS_OVERLAPPEDWINDOW,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  43.                           NULL, NULL, hInstance, NULL) ;
  44.  
  45.      ShowWindow (hwnd, iCmdShow) ;
  46.      UpdateWindow (hwnd) ;
  47.  
  48.      while (GetMessage (&msg, NULL, 0, 0))
  49.      {
  50.           TranslateMessage (&msg) ;
  51.           DispatchMessage (&msg) ;
  52.      }
  53.      return msg.wParam ;
  54. }
  55.  
  56. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  57. {
  58.      static HBITMAP      hBitmap ;
  59.      static HPALETTE     hPalette ;
  60.      static int          cxClient, cyClient ;
  61.      static OPENFILENAME ofn ;
  62.      static PBYTE        pBits ;
  63.      static TCHAR        szFileName [MAX_PATH], szTitleName [MAX_PATH] ;
  64.      static TCHAR        szFilter[] = TEXT ("Bitmap Files (*.BMP)\0*.bmp\0")
  65.                                       TEXT ("All Files (*.*)\0*.*\0\0") ;
  66.      BITMAP              bitmap ;
  67.      BITMAPINFO        * pPackedDib ;
  68.      HDC                 hdc, hdcMem ;
  69.      PAINTSTRUCT         ps ;
  70.  
  71.      switch (message)
  72.      {
  73.      case WM_CREATE:
  74.           ofn.lStructSize       = sizeof (OPENFILENAME) ;
  75.           ofn.hwndOwner         = hwnd ;
  76.           ofn.hInstance         = NULL ;
  77.           ofn.lpstrFilter       = szFilter ;
  78.           ofn.lpstrCustomFilter = NULL ;
  79.           ofn.nMaxCustFilter    = 0 ;
  80.           ofn.nFilterIndex      = 0 ;
  81.           ofn.lpstrFile         = szFileName ;
  82.           ofn.nMaxFile          = MAX_PATH ;
  83.           ofn.lpstrFileTitle    = szTitleName ;
  84.           ofn.nMaxFileTitle     = MAX_PATH ;
  85.           ofn.lpstrInitialDir   = NULL ;
  86.           ofn.lpstrTitle        = NULL ;
  87.           ofn.Flags             = 0 ;
  88.           ofn.nFileOffset       = 0 ;
  89.           ofn.nFileExtension    = 0 ;
  90.           ofn.lpstrDefExt       = TEXT ("bmp") ;
  91.           ofn.lCustData         = 0 ;
  92.           ofn.lpfnHook          = NULL ;
  93.           ofn.lpTemplateName    = NULL ;
  94.  
  95.           return 0 ;
  96.  
  97.      case WM_SIZE:
  98.           cxClient = LOWORD (lParam) ;
  99.           cyClient = HIWORD (lParam) ;
  100.           return 0 ;
  101.  
  102.      case WM_COMMAND:
  103.           switch (LOWORD (wParam))
  104.           {
  105.           case IDM_FILE_OPEN:
  106.  
  107.                     // Show the File Open dialog box
  108.  
  109.                if (!GetOpenFileName (&ofn))
  110.                     return 0 ;
  111.                
  112.                     // If there's an existing packed DIB, free the memory
  113.  
  114.                if (hBitmap)
  115.                {
  116.                     DeleteObject (hBitmap) ;
  117.                     hBitmap = NULL ;
  118.                }
  119.                
  120.                     // If there's an existing logical palette, delete it
  121.  
  122.                if (hPalette)
  123.                {
  124.                     DeleteObject (hPalette) ;
  125.                     hPalette = NULL ;
  126.                }
  127.                
  128.                     // Load the packed DIB into memory
  129.  
  130.                SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  131.                ShowCursor (TRUE) ;
  132.  
  133.                pPackedDib = PackedDibLoad (szFileName) ;
  134.  
  135.                ShowCursor (FALSE) ;
  136.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  137.  
  138.                if (pPackedDib)
  139.                {
  140.                          // Create the DIB section from the DIB
  141.  
  142.                     hBitmap = CreateDIBSection (NULL,
  143.                                              pPackedDib, 
  144.                                              DIB_RGB_COLORS,
  145.                                              &pBits, 
  146.                                              NULL, 0) ;
  147.  
  148.                          // Copy the bits
  149.  
  150.                     CopyMemory (pBits, PackedDibGetBitsPtr  (pPackedDib),
  151.                                        PackedDibGetBitsSize (pPackedDib)) ;
  152.  
  153.                          // Create palette from the DIB
  154.  
  155.                     hPalette = PackedDibCreatePalette (pPackedDib) ;
  156.  
  157.                          // Free the packed-DIB memory
  158.  
  159.                     free (pPackedDib) ;
  160.                }
  161.                else
  162.                {
  163.                     MessageBox (hwnd, TEXT ("Cannot load DIB file"), 
  164.                                 szAppName, 0) ;
  165.                }
  166.                InvalidateRect (hwnd, NULL, TRUE) ;
  167.                return 0 ;
  168.           }
  169.           break ;
  170.  
  171.      case WM_PAINT:
  172.           hdc = BeginPaint (hwnd, &ps) ;
  173.  
  174.           if (hPalette)
  175.           {
  176.                SelectPalette (hdc, hPalette, FALSE) ;
  177.                RealizePalette (hdc) ;
  178.           }
  179.           if (hBitmap)
  180.           {
  181.                GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
  182.  
  183.                hdcMem = CreateCompatibleDC (hdc) ;
  184.                SelectObject (hdcMem, hBitmap) ;
  185.  
  186.                BitBlt (hdc,    0, 0, bitmap.bmWidth, bitmap.bmHeight, 
  187.                        hdcMem, 0, 0, SRCCOPY) ;
  188.  
  189.                DeleteDC (hdcMem) ;
  190.           }
  191.           EndPaint (hwnd, &ps) ;
  192.           return 0 ;
  193.  
  194.      case WM_QUERYNEWPALETTE:
  195.           if (!hPalette)
  196.                return FALSE ;
  197.  
  198.           hdc = GetDC (hwnd) ;
  199.           SelectPalette (hdc, hPalette, FALSE) ;
  200.           RealizePalette (hdc) ;
  201.           InvalidateRect (hwnd, NULL, TRUE) ;
  202.  
  203.           ReleaseDC (hwnd, hdc) ;
  204.           return TRUE ;
  205.  
  206.      case WM_PALETTECHANGED:
  207.           if (!hPalette || (HWND) wParam == hwnd)
  208.                break ;
  209.  
  210.           hdc = GetDC (hwnd) ;
  211.           SelectPalette (hdc, hPalette, FALSE) ;
  212.           RealizePalette (hdc) ;
  213.           UpdateColors (hdc) ;
  214.  
  215.           ReleaseDC (hwnd, hdc) ;
  216.           break ;
  217.  
  218.           
  219.      case WM_DESTROY:
  220.           if (hBitmap)
  221.                DeleteObject (hBitmap) ;
  222.  
  223.           if (hPalette)
  224.                DeleteObject (hPalette) ;
  225.  
  226.           PostQuitMessage (0) ;
  227.           return 0 ;
  228.      }
  229.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  230. }
  231.  
  232.